<?php
/**
 * 
 * model() 帮助我们创建模型队形 
 * tableName() 帮助我们返回当前数据表的名字 是静态方法
 * @author Administrator
 *
 */
class detaillist extends CActiveRecord {
	// classname就是当前class 名字 chapters
	// 反悔当前模型对象的静态方法 固定的
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	
	/**
	 * 返回当前数据表的名字
	 */
	public function tableName() {
		// 如果有前缀可以加两个括号 这样会自动关联
		// return '{{chapters}}';
		return 'detaillist';
	}
	
	/*  public function getMangas() {
		return $this->hasMany(mangas::className(), ['id' => 'mid'])
		->viaTable('mangascategories', ['cid' => 'id']);
	}  */
	
	function getMangasInfoByCid($cid, $status, $type, $page, $pageSize){
		//把获得的具体详细商品信息存入缓存，下次再来获得信息就去缓存读取
// 		$info = Yii::app()->cache->get('goods_info'.$id);//获得缓存信息
	
		//判断缓存信息有无
// 		if(!empty($info))
// 			return $info;

		echo "cid is $cid";
		
		if(empty($cid)) {
			$cid = "";
		}
		
		if(empty($page)) {
			$page = 1;
		}
		
		$order = "";
		if($type == 1) {
			$order = 'order by m.title asc';
		} else if($type == 2) {
			$order = 'order by m.views desc';
		} else if($type == 3) {
			$order = 'order by m.score desc';
		} else if($type == 4) {
			$order = 'order by m.timestamp desc';
		} else {
			$order = 'order by m.id desc';
		}
		
		$condition = "";
		$table = "`mangas` m";
		if(!empty($cid)) {
			$condition = "mc.cid = $cid AND mc.mid = m.id";
		}
		
		if(!empty($condition)) {
			$condition = "where $condition";
			$table = "`mangascategories` mc, `mangas` m";
		}
		
		$beginPage = $page * $pageSize;
		$sql = "SELECT * FROM $table $condition $order limit $beginPage, $pageSize";
		$mangasInfo = $this->findAllBySql($sql);
		echo $sql;
		//设置缓存
		//Yii::app()->cache->set('goods_info'.$id,$mangasInfo,3600);
	
		return $mangasInfo;
	}
}